import { createApp } from 'vue'
import Message from './Message.vue'
export type MessageType = 'success' | 'error' | 'default'

// message 为显示什么样的信息，MessageType为显示什么样的信息，timeout多久消失，默认2秒
const createMessage = (message: string, type: MessageType, timeout = 2000) => {
  // createApp 接收两个参数，第一个是对象组件：Message，第二个是组件的props
  // 创建
  const messageInstance = createApp(Message, {
    message,
    type
  })
  // 挂载
  const mountNode = document.createElement('div')
  document.body.appendChild(mountNode)
  messageInstance.mount(mountNode)
  // 定时清空
  setTimeout(() => {
    messageInstance.unmount()
    document.body.removeChild(mountNode)
  }, timeout)
}

export default createMessage
